#include <iostream>
#include <string>
#include <fstream>
#include <boost/program_options.hpp>

#include "include/Configuration.h"

namespace po = boost::program_options;

void Configuration::Init(string & config_file_name)
{
	_config_file_name = config_file_name;
}


int Configuration::ReadConfig()
{
	ifstream ifs(_config_file_name.c_str());
	if (!ifs)
	{
		cout << "can not open config file: " << _config_file_name << endl;
		return 1;
	}

	po::options_description config("Configuration");
	config.add_options()
		("port", po::value<int>(&port), "server port")
		("control_port", po::value<int>(&control_port), "control port")
		("thread_count", po::value<int>(&thread_count), "thread count")
		("protocol", po::value<int>(&protocol), "data package protocol")
		("version", po::value<int>(&version), "data package protocol version")
		("delay_buffer_num", po::value<int>(&delay_buffer_num), "delay buffer num")
		("msg_queue_name", po::value<string>(), "msg queue num")
		("first_dll_path", po::value<string>(), "first_dll_path")
		("second_dll_path", po::value<string>(), "second_dll_path")
		("first_dll_func", po::value<string>(), "first_dll_func")
		("second_dll_func", po::value<string>(), "second_dll_func")
		("dll_init_func", po::value<string>(), "dll_init_func")
		("dll_exit_func", po::value<string>(), "dll_exit_func")
		("game_config_file", po::value<string>(), "game_config_file")
		;

	po::options_description config_file_options;
	config_file_options.add(config);

	po::variables_map vm;
	store(parse_config_file(ifs, config_file_options), vm);
	notify(vm);

	if(vm.count("port"))
	{
		cout << "port: " << port << endl;
	}
	else
	{
		cout << "miss configuration: port" << endl;
		return 1;
	}

	if(vm.count("control_port"))
	{
		cout << "control port: " << control_port << endl;
	}
	else
	{
		cout << "miss configuration: control port" << endl;
		return 1;
	}

	if(vm.count("thread_count"))
	{
		cout << "thread_count: " << thread_count << endl;
	}
	else
	{
		cout << "miss configuration: thread count" << endl;
		return 1;
	}


	if(vm.count("protocol"))
	{
		cout << "protocol: " << protocol << endl;
	}
	else
	{
		cout << "miss configuration: protocol" << endl;
		return 1;
	}

	if(vm.count("version"))
	{
		cout << "version: " << version << endl;
	}
	else
	{
		cout << "miss configuration: version" << endl;
		return 1;
	}

	if(vm.count("delay_buffer_num"))
	{
		cout << "delay_buffer_num: " << delay_buffer_num << endl;
	}
	else
	{
		cout << "miss configuration: delay_buffer_num" << endl;
		return 1;
	}

	if(vm.count("msg_queue_name"))
	{
		msg_queue_name = vm["msg_queue_name"].as<string>();	
		cout << "msg_queue_name: " << msg_queue_name << endl;
	}
	else
	{
		cout << "miss configuration: msg_queue_name" << endl;
		return 1;
	}

	if(vm.count("first_dll_path"))
	{
		first_dll_path = vm["first_dll_path"].as<string>();	
		cout << "first_dll_path: " << first_dll_path << endl;
	}
	else
	{
		cout << "miss configuration: first_dll_path" << endl;
		return 1;
	}

	if(vm.count("second_dll_path"))
	{
		second_dll_path = vm["second_dll_path"].as<string>();	
		cout << "second_dll_path: " << second_dll_path << endl;
	}
	else
	{
		cout << "miss configuration: second_dll_path" << endl;
		return 1;
	}

	if(vm.count("first_dll_func"))
	{
		first_dll_func = vm["first_dll_func"].as<string>();	
		cout << "first_dll_func: " << first_dll_func << endl;
	}
	else
	{
		cout << "miss configuration: first_dll_func" << endl;
		return 1;
	}

	if(vm.count("second_dll_func"))
	{
		second_dll_func = vm["second_dll_func"].as<string>();	
		cout << "second_dll_func: " << second_dll_func << endl;
	}
	else
	{
		cout << "miss configuration: second_dll_func" << endl;
		return 1;
	}

	if(vm.count("dll_init_func"))
	{
		dll_init_func = vm["dll_init_func"].as<string>();	
		cout << "dll_init_func: " << dll_init_func << endl;
	}
	else
	{
		cout << "miss configuration: dll_init_func" << endl;
		return 1;
	}

	if(vm.count("dll_exit_func"))
	{
		dll_exit_func = vm["dll_exit_func"].as<string>();	
		cout << "dll_exit_func: " << dll_exit_func << endl;
	}
	else
	{
		cout << "miss configuration: dll_exit_func" << endl;
		return 1;
	}
	
	if(vm.count("game_config_file"))
	{
		game_config_file = vm["game_config_file"].as<string>();	
		cout << "dll_exit_func: " << game_config_file << endl;
	}
	else
	{
		cout << "miss configuration: game_config_file" << endl;
		return 1;
	}

	return 0;
}
